package cn.jiyun.leyou.goods.service;

import cn.jiyun.leyou.goods.feign.BrandClient;
import cn.jiyun.leyou.goods.feign.CategoryClient;
import cn.jiyun.leyou.goods.feign.GoodsClient;
import cn.jiyun.leyou.goods.feign.SpecClient;
import com.leyou.item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodsService {

    @Autowired
    BrandClient brandClient;
    @Autowired
    CategoryClient categoryClient;
    @Autowired
    GoodsClient goodsClient;
    @Autowired
    SpecClient specClient;

    public Map<String, Object> buildGoods(Long id) {


        Spu spu = goodsClient.getSpus(id);

        SpuDetail spuDetail = goodsClient.getSpuDetail(id);

        List<Sku> skus = goodsClient.getSkus(id);

        List<Long> ids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<String> names = categoryClient.getNamesByIds(ids);

        List<Map<String,Object>> categories = new ArrayList<>();
        for (int i = 0; i < names.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",ids.get(i));
            map.put("name",names.get(i));
            categories.add(map);
        }

        Brand brand = brandClient.getBrandByBrandId(spu.getBrandId());

        // 查询的所有的规格参数
        List<SpecGroup> groups = specClient.groupsAndParam(spu.getCid3());

        // skus中显示，特殊的规格参数，用于渲染选择项的标题
        List<SpecParam> params = specClient.params(null, spu.getCid3(), null, false);
        // {"id":"颜色","id":"内存"}
        Map<Long,String> paramMap = new HashMap<>();
        params.forEach(p->{
            paramMap.put(p.getId(),p.getName());
        });

        Map<String,Object> map = new HashMap<>();

        // 封装spu
        map.put("spu", spu);
        // 封装spuDetail
        map.put("spuDetail", spuDetail);
        // 封装sku集合
        map.put("skus", skus);
        // 分类
        map.put("categories", categories);
        // 品牌
        map.put("brand", brand);
        // 规格参数组  ?  list  规格参数
        map.put("groups", groups);
        // 查询特殊规格参数
        map.put("params", paramMap);
        return map;

    }
}
